# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# @Create Time  : 2024/4/2 17:43
# @Author       : kony.xie
# @File Name    : models.py
# @Python Ver.  : Python 3.8.10
# @Description  : Database model, responsible for the mapping between business objects and the database(ORM).
#                 Through orm, objects can directly operate the database without sql.
# -------------------------------------------------------------------------------
from django.db import models
from django.db.models import UniqueConstraint
from django.db.backends.mysql.base import DatabaseWrapper

DatabaseWrapper.data_types['DateTimeField'] = "datetime"

# Create your models here.

class Project(models.Model):
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    description = models.CharField(max_length=255, null=True, help_text="Project description")
    cron = models.CharField(max_length=255, default=None, help_text="Cron expression")
    mails = models.CharField(max_length=255, default=None, help_text="Email address")
    engine = models.CharField(max_length=10, default=None, help_text="Test tool")
    owner = models.CharField(max_length=32, default=None, help_text="Project owner")
    workers = models.CharField(max_length=100, default=None, help_text="IP list of the load machines")
    status = models.BooleanField(default=0, help_text="Status flag bits, 0 is idle, 1 is running")
    ident = models.BigIntegerField(null=True, help_text="Thread identifier")
    create_time = models.DateTimeField(auto_now_add=True, help_text="Creation time")

    def __str__(self):
        return self.project
    
class Scene(models.Model):
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    scene_type = models.CharField(max_length=10, default=None, help_text="Test scenarios")
    params = models.CharField(max_length=255, default=None, help_text="Custom parameters")
    threshold = models.DecimalField(max_digits=5, default=None, decimal_places=2, help_text="Error threshold")
    trans_name = models.CharField(max_length=32, default=None, help_text="Transaction name")
    threads = models.CharField(max_length=255, default=None, help_text="Thread gradient")
    duration = models.IntegerField(default=None, help_text="Duration (s)")
    create_time = models.DateTimeField(auto_now_add=True, help_text="Creation time")
    status = models.BooleanField(default=0, help_text="Status flag bits, 0 is idle, 1 is running")

    def __str__(self):
        return self.project

class File(models.Model):
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    file_name = models.CharField(max_length=32, default=None, help_text="File name")
    path = models.CharField(max_length=255, help_text="File absolute path")
    upload_time = models.DateTimeField(auto_now=True, help_text="Upload time")

    def __str__(self):
        return self.project

class Result(models.Model):
    # 文件名称
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    tran_name = models.CharField(max_length=32, default=None, help_text="Transaction name")
    file_name = models.CharField(max_length=100, default=None, help_text="File name")
    path = models.CharField(max_length=255, help_text="File absolute path")
    create_time = models.DateTimeField(auto_now_add=True, help_text="Creation time")

    def __str__(self):
        return self.project

class Baseline(models.Model):
    # 基线数据表
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    tran_name = models.CharField(max_length=32, default=None, help_text="Transaction name")
    thread = models.IntegerField(default=None, help_text="Thread count")
    tps = models.FloatField(default=None, help_text="TPS")
    response_time = models.FloatField(default=None, help_text="Avg response time")

    class Meta:
        constraints = [
            UniqueConstraint( fields=['project', 'tran_name', 'thread'], name='unique_scenario' ),
        ]

    def __str__(self):
        return self.project

class Worker(models.Model):
    # 文件名称
    project = models.CharField(max_length=32, default=None, help_text="Project name")
    ip = models.CharField(max_length=32, default=None, help_text="IP of the load machine")
    username = models.CharField(max_length=32, default=None, help_text="Username of the load machine")
    password = models.CharField(max_length=32, default=None, help_text="Password of the load machine")

    def __str__(self):
        return self.project

class Job(models.Model):
    id = models.CharField(max_length=32, default=None, primary_key=True, help_text="job id")
    next_run_time = models.IntegerField(default=None, help_text="Next run time")
    job_state = models.BinaryField(default=None, help_text="Job state")

    def __str__(self):
        return self.id